package com.hhang.boot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hhang.boot.dao.HouseDao;
import com.hhang.boot.exception.ServiceException;
import com.hhang.boot.service.HouseService;
import com.hhang.boot.utils.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseDao houseDao;


    //茶室部分
    @Override
    public PageInfo<Map<String, Object>> findHouse(Map<String, Object> param) throws ServiceException {
        try {
            Object pObj = param.get("page");
            Object sObj = param.get("size");
            Integer page = 1;
            Integer size = ConstantUtil.SIZE;
            if (pObj != null) {
                page = Integer.valueOf((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.valueOf((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> CommList = houseDao.findHouse(param);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(CommList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }
    @Override
    public int addHouse(Map<String, Object> param) throws ServiceException {
        try {
            return houseDao.addHouse(param);
        } catch (Exception e) {
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }
    @Override
    public int delHouse(List<Long> ids) throws ServiceException {
        try {
            return houseDao.delHouse(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除失败", e);
        }
    }
    @Override
    public int upHouse(Map<String, Object> param) throws ServiceException {
        int res = -3;
        try {
            res = houseDao.upHouse(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("服务异常，修改失败",e);
        }
        if (res == 0){
            throw new ServiceException("修改失败，请刷新重试");
        }
        return res;
    }


    //分店部分
    @Override
    public PageInfo<Map<String, Object>> findBranch(Map<String, Object> param) throws ServiceException {
        try {
            Object pObj = param.get("page");
            Object sObj = param.get("size");
            int page = 1;
            int size = ConstantUtil.SIZE;
            if (pObj != null) {
                page = Integer.parseInt((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.parseInt((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> branchList = houseDao.findBranch(param);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(branchList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }
    @Override
    public int delBranch(List<Long> ids) throws ServiceException {
        try {
            return houseDao.delBranch(ids);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除失败", e);
        }
    }

    @Override
    public int addBranch(Map<String, Object> param) throws ServiceException {
        try {
            return houseDao.addBranch(param);
        } catch (Exception e) {
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }


    @Override
    public int add(Map<String, Object> param) throws ServiceException {
        try {
            return houseDao.add(param);
        } catch (Exception e) {
            throw new RuntimeException("error_s数据提交失败",e);
        }
    }
    @Override
    public int del(List<Long> id) throws ServiceException {
        try {
            return houseDao.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("删除失败", e);
        }
    }


    @Override
    public int update(Map<String, Object> param) throws ServiceException {
        int res = -2;
        try {
            res = houseDao.update(param);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("数据异常",e);
        }
        if (res == 0){
            throw new ServiceException("修改失败，请刷新重试");
        }
        return res;
    }

    @Override
    public PageInfo<Map<String, Object>> findPrice(Map<String, Object> notes) throws ServiceException {
        try {
            Object pObj = notes.get("page");
            Object sObj = notes.get("size");
            Integer page = 1;
            Integer size = ConstantUtil.SIZE;
            if (pObj != null) {
                page = Integer.valueOf((String) pObj);
                if (page < 1) {
                    page = 1;
                }
            }
            if (sObj != null) {
                size = Integer.valueOf((String) sObj);
            }
            PageHelper.startPage(page, size);
            List<Map<String, Object>> notesList = houseDao.findPrice(notes);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(notesList);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }

    @Override
    public List<Map<String, Object>> find7days(Map<String, Object> notes) throws ServiceException {
        try {
            return houseDao.find7days(notes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("error_s服务查询异常", e);
        }
    }


}
